package com.jhd.fuzhou.view.archive;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.adapter.ArchiveAdapter;
import com.jhd.fuzhou.service.ArchiveService;
import com.jhd.fuzhou.util.UtilHelper;
import com.jhd.fuzhou.vo.ArchivesFileVo;
import com.jhd.fuzhou.vo.JsonForResult;

import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class ArchiveDocumentActivity extends BaseActivity {
    @BindView(R.id.recyclerView)RecyclerView recyclerView;
    private Context context=this;
    private List<ArchivesFileVo> archivesFileVoList;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_punishment_document);
    }

    @Override
    protected void processLogic() {
        onBack();
        String id=getIntent().getStringExtra("id");
        getArchivesFilesById(id);
    }
    public void getArchivesFilesById(String id){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        ArchiveService vs = retrofit.create(ArchiveService.class);
        Map<String, String> params = ParamsMapUtil.getInstance("archivesId",id);
        Call<JsonForResult> jsonForResultCall = vs.getArchivesFilesById(application.getAccessToken(),params);//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        if(jsonForResult.getData()!=null){
                            archivesFileVoList = JSON.parseArray(jsonForResult.getData().toString(), ArchivesFileVo.class);
                            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
                            recyclerView.setLayoutManager(linearLayoutManager);
                            ArchiveAdapter adapter=new ArchiveAdapter(archivesFileVoList,context);
                            recyclerView.setAdapter(adapter);
                        }else{
                            archivesFileVoList=new ArrayList<>();
                        }
                    } else {
                        UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                    }
                } catch (Exception e) {
//                    CustomToast.showText(context,e.getLocalizedMessage(),3000,0,0).show();
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络异常!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg;
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }
}